import bpy
#import Blender
import math, sys
from math import *

PATH="/home/nines/blender/"
#Objeto seleccionado
object = bpy.context.selected_objects

objectname= object[0].name
meshname = object[0].data.name
print (meshname)
#obtenemos la malla de un nombre dado.
mesh=bpy.data.meshes[meshname]

#mesh= object.create_mesh



#object=Object.GetSelected()
#objname = bpy.data.objects[1].name
#meshname= object[0].data.name
#mesh= NMesh.GetRaw(meshname)

filename= PATH + objectname+ ".obj"
file=open(filename,"w")
std=sys.stdout
sys.stdout=file
print ("# objeto orej:", objectname)
print ("# Vertices Totales:", len(mesh.verts))
print ("# Cara Totales:", len(mesh.faces))

#Escribimos todas la lista de vertices( v x y z)
for vertex in mesh.verts:
    x,y,z = vertex.co
    print ("v %f %f %f" % (x,y,z))

#Escribimos todas la lista de caras (f v1 v2 v3 ....)

    
for face in mesh.faces:
    i=len(face.verts)-1
    j=0
    vertices=""
    for vertex in face.verts:
        vertices=vertices+str(face.verts[i-j]+1)+" "
        j = j+1
    print ("f",vertices)
#for vertex in face.verts:
#print vertex.index+1,
  #         print )

#for face in mesh.faces:
 #   nvertex=0
  #  i=3
   # print ("t",)
    #for vertex in face.verts:
#		print (face.uv[nvertex][0], face.uv.[nvertex][1],)
	#		nvertex = nvertex + 1
#	print
sys.stdout = std
file.close()
print ("Fichero salvado:", filename)